package cn.iocoder.yudao.module.biz.controller.admin.expand.targets.partner;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

import cn.iocoder.yudao.module.biz.controller.admin.expand.targets.partner.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.expand.targets.partner.ExpandTargetPartnerDO;
import cn.iocoder.yudao.module.biz.service.expand.targets.partner.ExpandTargetPartnerService;

@Tag(name = "管理后台 - 拓展目标_合作伙伴")
@RestController
@RequestMapping("/san/biz-expand-target-partner")
@Validated
public class ExpandTargetPartnerController {

    @Resource
    private ExpandTargetPartnerService expandTargetPartnerService;

    @PostMapping("/create")
    @Operation(summary = "创建拓展目标_合作伙伴")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-target-partner:create')")
    public CommonResult<Long> createExpandTargetPartner(@Valid @RequestBody ExpandTargetPartnerSaveReqVO createReqVO) {
        return success(expandTargetPartnerService.createExpandTargetPartner(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新拓展目标_合作伙伴")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-target-partner:update')")
    public CommonResult<Boolean> updateExpandTargetPartner(@Valid @RequestBody ExpandTargetPartnerSaveReqVO updateReqVO) {
        expandTargetPartnerService.updateExpandTargetPartner(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除拓展目标_合作伙伴")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('san:biz-expand-target-partner:delete')")
    public CommonResult<Boolean> deleteExpandTargetPartner(@RequestParam("id") Long id) {
        expandTargetPartnerService.deleteExpandTargetPartner(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除拓展目标_合作伙伴")
                @PreAuthorize("@ss.hasPermission('san:biz-expand-target-partner:delete')")
    public CommonResult<Boolean> deleteExpandTargetPartnerList(@RequestParam("ids") List<Long> ids) {
        expandTargetPartnerService.deleteExpandTargetPartnerListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得拓展目标_合作伙伴")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-target-partner:query')")
    public CommonResult<ExpandTargetPartnerRespVO> getExpandTargetPartner(@RequestParam("id") Long id) {
        ExpandTargetPartnerDO expandTargetPartner = expandTargetPartnerService.getExpandTargetPartner(id);
        return success(BeanUtils.toBean(expandTargetPartner, ExpandTargetPartnerRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得拓展目标_合作伙伴分页")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-target-partner:query')")
    public CommonResult<PageResult<ExpandTargetPartnerRespVO>> getExpandTargetPartnerPage(@Valid ExpandTargetPartnerPageReqVO pageReqVO) {
        PageResult<ExpandTargetPartnerDO> pageResult = expandTargetPartnerService.getExpandTargetPartnerPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ExpandTargetPartnerRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出拓展目标_合作伙伴 Excel")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-target-partner:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportExpandTargetPartnerExcel(@Valid ExpandTargetPartnerPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ExpandTargetPartnerDO> list = expandTargetPartnerService.getExpandTargetPartnerPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "拓展目标_合作伙伴.xls", "数据", ExpandTargetPartnerRespVO.class,
                        BeanUtils.toBean(list, ExpandTargetPartnerRespVO.class));
    }

    @GetMapping("/checkExistByName")
    @Operation(summary = "校验合作伙伴是否存在")
    @Parameter(name = "name", description = "合作伙伴名称", required = true, example = "合作伙伴名称")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-target-partner:query')")
    public CommonResult<Integer> checkExistByName(@RequestParam("name") String name) {
        Integer existFlag = expandTargetPartnerService.checkExistByName(name.trim());
        return success(existFlag);
    }

}